package com.ld.security.common.quartz;//package com.ld.security.common.quartz.config;

import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.ld.security.common.mybatis.util.DataSourceUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.quartz.Calendar;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 覆盖配置类
 */
@Configuration
@MapperScan("com.ld.security.common.quartz.modules.*.mapping")
@ComponentScan
public class EnableJobConfig extends QuartzAutoConfiguration {

    public EnableJobConfig(QuartzProperties properties, ObjectProvider<SchedulerFactoryBeanCustomizer> customizers, ObjectProvider<JobDetail[]> jobDetails, ObjectProvider<Map<String, Calendar>> calendars, ObjectProvider<Trigger[]> triggers, ApplicationContext applicationContext) {
        super(properties, customizers, jobDetails, calendars, triggers, applicationContext);
    }


    @Autowired
    DynamicDataSourceProvider dynamicDataSourceProvider;


    /**
     * 覆盖数据源
     * @return
     */
    public SchedulerFactoryBean quartzScheduler(){
        SchedulerFactoryBean schedulerFactoryBean =  super.quartzScheduler();
        DataSource dataSource =    DataSourceUtil.getDataSource("quartz");
        schedulerFactoryBean.setDataSource(dataSource);

        return  schedulerFactoryBean;
    }
}